#随机森林 根据gra删除特征
import numpy as np
import scipy
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier

from sklearn.model_selection import train_test_split,cross_val_score,GridSearchCV
from sklearn.metrics import roc_curve,auc,roc_auc_score

import matplotlib.pyplot as plt
import time

from FastApiGRA import gra_fastApi
from GRA_improve import gra_imporve


# file_data = 'sample_result.csv'

# 数据读取    skiprows跳过行
def c(a,file_data,n):
    # result = 0
    #
    # x = np.loadtxt(file_data, dtype=float, delimiter=',', usecols=(a))
    # y = np.loadtxt(file_data, dtype=str, delimiter=',', usecols=(20,))
    #
    # seed = 5
    # xtrain, xtest, ytrain, ytest = train_test_split(x, y, test_size=0.3, random_state=seed)
    #
    # rfc = RandomForestClassifier()
    # rfc = rfc.fit(xtrain, ytrain)
    #
    # result += rfc.score(xtest, ytest)
    # return result
    result=0
    for i in range(0,5):
        x = np.loadtxt(file_data, dtype=float, delimiter=',',usecols=(a))
        y = np.loadtxt(file_data, dtype=str, delimiter=',', usecols=(n-1,))

        seed =5
        xtrain , xtest,ytrain,ytest=train_test_split(x,y,test_size=0.3,random_state=seed)

        rfc =RandomForestClassifier()
        rfc=rfc.fit(xtrain,ytrain)

        result+=rfc.score(xtest,ytest)
    return result/5


# 数据读取    skiprows跳过行
# def c(a):
#
#     x = np.loadtxt(file_data, dtype=float, delimiter=',',usecols=(a))
#     y = np.loadtxt(file_data, dtype=str, delimiter=',', usecols=(20,))
#
#     seed =5
#     xtrain , xtest,ytrain,ytest=train_test_split(x,y,test_size=0.3,random_state=seed)
#
#     rfc =RandomForestClassifier()
#     rfc=rfc.fit(xtrain,ytrain)
#
#     result=rfc.score(xtest,ytest)
#     return result

# if __name__ == '__main__':
#     a = [2, 1, 4, 8, 10, 3, 9, 7, 18, 5, 13, 0, 19, 17, 14, 12, 15, 6, 16, 11]
#     for i in range(18):
#         a.pop(0)
#         print(a)
#         start = time.perf_counter()
#         print('Accuracy = %.2f %%' % (c(a)*100))
#         end = time.perf_counter()
#         print('时间：', end - start)








tol=0.00001
def cc(S,F,file_data,n):
    Score = c(S,file_data,n)
    i=0
    while i<len(F):
        S.remove(F[i])
        S_new=S
        print("s_new",S_new)
        S=S_new+[F[i]]
        print("S",S)
        if len(S_new)==0:
            S_op=S
            i=len(F)+1
            print("S_op1")
            print(S_op)
        else:
            # print(S_new)
            Score_new=c(S_new,file_data,n)
            print("旧", Score)
            print("新",Score_new)

            if i== len(F)-1 and Score_new<Score +tol:
                print("最终准确率",Score)
                S_op=S
                i=len(F)+1
                print("S_op2")
                print(S_op)
                return  S_op

            else:
                i+=1
                if(Score_new>=Score+tol):
                    Score=Score_new
                    F.pop(i - 1)
                    S = S_new
                    print("ccc")
                    # print(S)
                    i = 0
                    # break
                    # while Score_new>=Score+tol:
                    #
                    #     F.pop(i-1)
                    #     S=S_new
                    #     print("ccc")
                    #     # print(S)
                    #     i=0
                    #     break



def sbfi(address):
    print(gra_fastApi(address))
    print(type(gra_fastApi(address)))
    F=gra_fastApi(address)
    F.reverse()
    print(F)
    file_data = address
    S = []
    data = pd.read_csv(address, encoding='GBk', header=None)
    label_need = data.keys()[:]
    data1 = data[label_need].values
    [m, n] = data1.shape
    for i in range(0,n-1):
        S.append(i)
    print(S)

    # F = [5, 15, 12, 14, 17, 9, 10, 3, 19, 18, 4, 13, 1, 2, 8, 7, 6, 16, 11, 0]
    # F = [0, 11, 16, 6, 7, 8, 2, 1, 13, 4, 18, 19, 3, 10, 9, 17, 14, 12, 15, 5]
    # F=['甜','红','大']
    # F=['1','2','0']




    return cc(S,F,file_data,n)


def chi2():
    print("success")
def randomforest():
    print("success")
def mic():
    print("success")
def mlp():
    print("success")
